// pages/index/detail.js
const app = getApp()
const Util = require('../../utils/util.js');
let wxparse = require("../../wxParse/wxParse.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationPop: {},
    popLogin: {},
    is_show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    if (e.id) {
      this.getGoodsDetail(e.id);
    }
    wx.getStorage({
      key: 'token',
      success: function (res) {
        Util.setReadList({
          token: res.data,
          id: e.id
        }, function (re) {

        })
      }, fail: function () {
        that.getMember();
      }
    });
  },
  bindButtonTap: function () {  //视频下载
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success: function (res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },
  getGoodsDetail: function (id) {
    console.info(id);
    var that = this;
    Util.getGoodsDetail({
      id: id
    }, function (re) {
      that.setData({
        goods_info: re.data
      })
      wxparse.wxParse('dkcontent', 'html', re.data.resume, that, 5);
    })
  },
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height;//图片高度
    var imgw = e.detail.width;//图片宽度
    var swiperH = (winWid * imgh / imgw - 5) + "px";
    this.setData({
      height: swiperH//设置高度
    })
  },
  getMember: function () {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        if (res.data) {
          that.setData({
            token: res.data,
            is_show: true
          });
        }
      }, fail: function () {
        that.checkMember();
      }
    })
  },
  checkMember: function () {
    var that = this;
    that.checkUser(-1, function (isauth) {
      console.info(isauth);
      if (isauth) {
        that.setData({
          is_show: true
        })
      } else {
        mytoast.toast();
        Util.showMsgDialog("请先授权再使用");
      }
    })
  },
  checkUser: function (action, cb) {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.info(res);
        if (res.data) {
          cb(true);
        }
      },
      fail: function () {
        Util.checkAuth(function (res) {
          if (!res.authSetting['scope.userInfo']) {
            //未授权用户
            that.showLogin();
          } else {
            app.getUserInfo(true, function (userInfo) {

              if (typeof cb == "function") {
                cb(true);
              }
            });
          }
        });
      }
    })
  },
  onGotUserInfo: function (e) {
    var that = this;
    if (e.detail.errMsg == 'getUserInfo:ok') {
      Util.showLoading('授权中');
      app.getUserInfo(true, function (userInfo) {
        if (typeof cb == "function") {
          cb(true);
        }
        that.setData({
          is_show: true
        });
        that.getMember();
      });
    }
    that.setData({
      popLoginStatus: false
    })
  },
  setPopStatus: function (e) {
    this.showPop(e.currentTarget.dataset.status);
  },
  showLogin: function () {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.scale(0).step()

    this.setData({
      popLogin: animation.export(),
      popLoginStatus: true
    })

    setTimeout(function () {
      animation.scale(1.0).step()
      this.setData({
        popLogin: animation
      })
    }.bind(this), 100)
  },
  showPop: function (status) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.scale(0).step()

    this.setData({
      animationPop: animation.export()
    })
    if (status == 1) {
      this.setData(
        {
          showPopStatus: true
        }
      );
    }
    setTimeout(function () {
      animation.scale(1.0).step()
      this.setData({
        animationPop: animation
      })
      if (status == 0) {
        this.setData(
          {
            showPopStatus: false,
          }
        );
      }
    }.bind(this), 300)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})